<template>
  <el-dialog
    :title="dialogProps.title"
    :visible.sync="dialogProps.visible"
    width="660px"
    @open="handleDialogOpen()"
  >
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div> 
    <el-form
      :inline="true"
      :model="templateModel"
      :rules="formRules"
      ref="tempForm"
      label-width="80px"
      label-position="right"
      size="small"
    >
      <el-tabs type="border-card" >
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题" prop="tempName">
              <el-input
                :maxlength="50"
                v-model="templateModel.tempName"
                autofocus
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关键词" prop="tempCrux">
              <el-input
                :maxlength="50"
                v-model="templateModel.tempCrux"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="适用人群" prop="intendedFor">
              <el-input
                :maxlength="50"
                v-model="templateModel.intendedFor"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="方案目标" prop="programmeObjectives">
              <el-input
                :maxlength="50"
                v-model="templateModel.programmeObjectives"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input
                class="properties-textarea"
                type="textarea"
                :maxlength="50"
                v-model="templateModel.remarks"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否禁用" prop="start">
              <el-switch
                v-model="templateModel.start"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="2"
                :inactive-value="1"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-tabs>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('tempForm')"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { UpdateTemplate, addTemplate } from "@/api/HealthIntervention/Template";

export default {
  name: "temp-edit",
  data() {
    return {
      templateModel: {
        tempId: 0,
        tempName: "",
        tempCrux: "",
        intendedFor: "",
        programmeObjectives: "",
        start: "1",
        remarks: "",
      },
      dialogProps: {
        visible: false,
        action: "",
        title: "",
      },
      //输入提示
      formRules: {
        // diaryPerson: [
        //   { required: true, message: '请输入名称', trigger: 'blur' },
        //   { min: 3, max: 32, message: '长度在 3 到 32 个字符', trigger: 'blur' }
        // ],
        // fileNumber: [
        //   { required: true, message: '请输入档案号', trigger: 'blur' },
        //   { min: 6, max: 32, message: '长度在 6 到 32 个字符', trigger: 'blur' }
        // ],
      },
    };
  },
  methods: {
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs["tempForm"].clearValidate();
      });
    },
    //判断是否是添加或修改
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.dialogProps.action === "add") {
            this.doSaveDiary();
          } else {
            this.doUpdateDiary();
          }
        } else {
          return false;
        }
      });
    },
    //关闭模态框
    handleDialogClose() {
      this.dialogProps.visible = false;
    },
    //编辑
    doUpdateDiary() {
      this.loading = true;
      UpdateTemplate(this.templateModel)
        .then((response) => {
          this.loading = false;
          this.dialogProps.visible = false;
          this.$emit("save-finished");
          this.$message({
            message: "修改成功",
            type: "success",
          });
        })
        .catch((error) => {
          this.outputError(error);
        });
    },
    //新增
    doSaveDiary() {
      this.loading = true;
      addTemplate(this.templateModel)
        .then((response) => {
          this.loading = false;
          this.dialogProps.visible = false;
          this.$emit("save-finished");
          this.$message({
            message: "新增成功",
            type: "success",
          });
        })
        .catch((error) => {
          this.outputError(error);
        });
    },

    outputError(error) {
      console.log(error.response ? error.response : error.message);
      this.loading = false;
      this.$message({
        showClose: true,
        message: "出错了，请按F12查看浏览器日志。",
        type: "error",
      });
    },
  },
  mounted: function () {
    this.$nextTick(() => {
      this.$on("openEditTemplateDialog", function (router) {
        this.templateModel = router;
        this.dialogProps.action = "edit";
        this.dialogProps.title = "编辑";
        this.dialogProps.visible = true;
      });
      this.$on("openAddTemplateDialog", function () {
        this.dialogProps.action = "add";
        this.dialogProps.title = "新增";
        this.templateModel = {
          indexStart: 1,
        };
        this.dialogProps.visible = true;
      });
    });
  },
};
</script>

<style lang="scss" scoped>
.properties-textarea {
  width: 496px;
}
</style>





